package com.tj.jinzao.base;



import android.text.TextUtils;
import android.util.Log;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallbackWrapper;
import com.netease.nimlib.sdk.uinfo.UserService;
import com.netease.nimlib.sdk.uinfo.constant.UserInfoFieldEnum;
import com.tj.jinzao.api.ApiRetrofit;
import com.tj.jinzao.api.ApiService;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.model.User;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public abstract class BasePresenter<V extends IBaseView> {

    protected V mView;
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();

    private CompositeSubscription mCompositeSubscription;

    public BasePresenter(V view) {
        attachView(view);
    }

    public void attachView(V view) {
        mView = view;
    }

    public void detachView() {
        mView = null;
        onUnsubscribe();
    }

    public void updateUserInfo(final User user){
        Map<UserInfoFieldEnum, Object> fields = new HashMap<>();
        fields.put(UserInfoFieldEnum.Name,user.getNickname());
        fields.put(UserInfoFieldEnum.AVATAR, Constant.IMAGE_PREFIX+user.getImgurl());
        NIMClient.getService(UserService.class).updateUserInfo(fields)
                .setCallback(new RequestCallbackWrapper<Void>() {
                    @Override
                    public void onResult(int i, Void aVoid, Throwable throwable) {
                    }
                });

    }

    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

    /**
     * 上传文件
     */
    public void uploadFile(final String localPath) {
        Log.w("数据接收图片上传","qqq"+localPath);
        //构建body
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        //文件参数
        if (!TextUtils.isEmpty(localPath)) {
            File file = new File(localPath);
            if (file.isFile()) {
                builder.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("*/*"), file));
            }
        } else {
            return;
        }
        RequestBody build = builder.build();
        addSubscription(ApiRetrofit.getInstance().getApiService().uploadFile(build), new SubscriberCallBack<String>(mView.getContext()) {
            @Override
            protected boolean isShowProgress() {
                return true;
            }

            @Override
            protected void onSuccess(String response) {
                Log.w("数据图片上传","hhhsss"+response);
                onUploadSuccess(localPath, response);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                Log.w("数据图片上传","hhh"+response.getMessage());
                onUploadFailed(localPath, response);
            }
        });
    }

    protected void onUploadSuccess(String localPath, String response) {


    }

    protected void onUploadFailed(String localPath, ResultResponse response) {

    }

}